package cn.pro.controller;

import cn.pro.api.IProRoleService;
import cn.pro.entity.ProRole;
import cn.pro.entity.qo.Query;
import cn.pro.entity.vo.ProRoleVo;
import cn.pro.utils.*;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/role")
public class ProRoleController {

    @Autowired
    private IProRoleService iProRoleService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public ProData save(@RequestBody ProRole proRole){
        proRole.setId(ObjectUtils.couponCodeTwo("pro_role"));
        proRole.setCreateTime(new Date());
        proRole.setUpdateTime(new Date());
        CheckMsg msg = iProRoleService.create(proRole);
        if (msg.isSuccess()){
            return ProData.data("200", VableUtil.SUSSES_STATUS,msg.getMesage());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,msg.getMesage());
        }
    }

    @RequestMapping(value = "/updata",method = RequestMethod.POST)
    public ProData modify(@RequestBody ProRole proRole){
        CheckMsg msg = iProRoleService.updateRole(proRole);
        if (msg.isSuccess()){
            return ProData.data("200", VableUtil.SUSSES_STATUS,msg.getMesage());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,msg.getMesage());
        }
    }

    @RequestMapping(value = "selectList",method = RequestMethod.POST)
    public ProData select(@RequestBody ProRole proRole){
        PageData<ProRoleVo> userData = iProRoleService.selectList(proRole);
        if (!org.springframework.util.ObjectUtils.isEmpty(userData)){
            Object obj = JSONArray.toJSON(userData);
            System.out.println("每页个数："+userData.getPageCount()+"  总数"+userData.getTotalCount());
            return ProData.data("200", VableUtil.SUSSES_STATUS,obj);
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,"查询异常");
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ProData deleteUser(@RequestBody Query pro){
        CheckMsg msg = iProRoleService.delete(pro.getId());
        if (msg.isSuccess()){
            return ProData.data("200", VableUtil.SUSSES_STATUS,msg.getMesage());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,msg.getMesage());
        }
    }

    @RequestMapping(value = "/select",method = RequestMethod.POST)
    public ProData selectUser(@RequestBody Query pro){
        ProRoleVo proRole = iProRoleService.selectById(pro.getId());
        if (!org.springframework.util.ObjectUtils.isEmpty(proRole)){
            return ProData.data("200", VableUtil.SUSSES_STATUS, proRole.toString());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,"查询异常");
        }
    }

    /*
     * 绑定功能
     * */
    @RequestMapping(value = "/bindAccess",method = RequestMethod.POST)
    public ProData bind(@RequestBody Query pro){
        CheckMsg msg = iProRoleService.bindAccess(pro.getId(),pro.getIds());
        if (msg.getMesage().contains("成功")){
            return ProData.data("200", VableUtil.SUSSES_STATUS,msg.getMesage());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,msg.getMesage());
        }
    }

    
}
